package com.panshi.controll;

import com.panshi.bean.PageUtil;
import com.panshi.bean.SearchBean;
import com.panshi.ISearchApi;
import com.panshi.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Api(value="搜索Controller层",tags={"搜索接口"})
@RequestMapping("sear")
@Controller
@CrossOrigin
public class SearchControll implements ISearchApi {
    @Resource
    private SearchService searchService;

    @PostMapping("/search")
    @ApiOperation("关键词搜索问题")
    @ResponseBody
    public PageUtil<List<SearchBean>> searchData(@RequestBody PageUtil pu) {
        pu.setStartIndex(pu.getStartIndexInfo());
        System.out.println(pu+"===========");


        //返回数据给前台
        List list=searchService.searchData(pu);
        int total=searchService.searchTotal(pu);

        PageUtil<List<SearchBean>> pu2=new PageUtil<>();
        pu2.setData(list);
        pu2.setTotal(total);

        System.out.println("查询出来的数据==="+pu2);

        return pu2 ;
    }

    @GetMapping("/hostSearch")
    @ApiOperation("热搜排行")
    @ResponseBody
    public List<SearchBean> hostSearch(){
        List<SearchBean> list=searchService.hostSearch();
        Collections.shuffle(list);//乱序输出
        return list;
    }

    @GetMapping("/index")
    public String indexData(@RequestParam String kw,Model model){
        PageUtil<List<SearchBean>> pu=new PageUtil<>();
        pu.setStartIndex(1);
        pu.setPageSize(10);
        pu.setKw(kw);
        pu.setStartIndex(pu.getStartIndexInfo());


        //返回数据给前台
        List list=searchService.searchData(pu);
        int total=searchService.searchTotal(pu);

        PageUtil<List<SearchBean>> pu2=new PageUtil<>();
        pu2.setData(list);
        pu2.setTotal(total);

        List<SearchBean> list2=searchService.hostSearch();
        Collections.shuffle(list2);//乱序输出

        model.addAttribute("pu",pu2);
        model.addAttribute("kw",kw);
        model.addAttribute("host",list2);
        return "search";
    }
}